package org.esti.model.dao.impl;

import org.esti.model.AppraisalTask;
import org.esti.model.dao.AppraisalTaskDAO;
import org.esti.model.exceptions.AppraisalTaskDoesNotExistException;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @author Artem Loginov (logart)
 */
@Repository
public class AppraisalTaskDAOimpl implements AppraisalTaskDAO {

    @PersistenceContext
    private EntityManager entityManager;

    /**
     * {@inheritDoc}
     */
    @Override
    public List<AppraisalTask> getAppraisalsTaskByAppraisalId(long appraisalId) {
        try {
            Query query = entityManager.createQuery(
                    "FROM AppraisalTask WHERE appraisal_id=:appraisalId"

            );
            query.setParameter("appraisalId", appraisalId);
            return (List<AppraisalTask>) query.getResultList();
        } catch (NoResultException ex) {
            throw new AppraisalTaskDoesNotExistException(ex);
        }
    }
}
